define([], function() {
    'use strict';
    /**
     * @param menus - beans菜单数组数据
     * @param key - 匹配的key，默认为url
     * @param showKey - 显示的字段的key，默认为name
     * @param value - 当前导航匹配的key的值
     * @param id - 要放置的容器ID
     * 
     * @function onItemClick - 面包屑item的点击事件
    */
    function BreadNav(option) {
        if(!option.id){
            throw new Error("请输入面包屑导航的容器id");
        }
        this.boxId = option.id;
        this.navBox = $("#" + option.id);
        if(this.navBox.length <= 0){
            throw new Error("输入的面包屑导航容器id不存在");
        }
        if(!option.menus){
            throw new Error("缺少菜单数据");
        }
        this.menus = option.menus || [];
        if(!option.value){
            throw new Error("缺少当前页面的value");
        }
        this.key = option.key || 'url';
        this.value = option.value || '';
        this.showKey = option.showKey || 'name';
        this.onItemClick = option.onItemClick;
        this.init();
    }

    BreadNav.prototype.init = function(){
        this.renderHtml();
        this.setListener();
    }

    BreadNav.prototype.getItemByKey = function(key){
        var that = this;
        var items = that.breadList.filter(function(item){
            return (item[that.key] + '') === key;
        })
        return items[0] || null;
    }

    BreadNav.prototype.setListener = function(){
        var that = this;
        this.navBox.on('click', '.bread-item', function(e){
            var _this = e.target;
            var key = $(_this).data('key');
            var aItem = that.getItemByKey(key);
            if(that.onItemClick){
                that.onItemClick(aItem);
            }
        })
    }

    /**
     * 渲染面包屑字符串
     */
    BreadNav.prototype.renderHtml = function(data){
        this.breadList = this.getBreadList(this.menus, this.value);
        var that = this;
        var str = '<div class="bread-nav-box"><div class="bread-nav">';
        this.breadList.forEach(function (item, index) {
            str += (index > 0 ? '<span class="bread-icon"> / </span>' : '')
                + '<span class="bread-item" data-key="'+ item[that.key] +'">'
                // + (!item.url ? item.name : ('<a href="' + item.url + '">' + item.name + '</a>'))
                + item[that.showKey]
                + '</span>';
        })
        str += '</div></div>';
        this.navBox.html(str);
    }

    /**
     * 获取面包屑的路径
     */
    BreadNav.prototype.getBreadList = function(menus, value){
        var that = this;
        var deepTraversal = function (menu, level) {
            level++;
            menu.children.forEach(function (item) {
                if (item.children) {
                    if(!flag){
                        list[level] = { name: item.name, url: '' };
                        deepTraversal(item, level)
                    }
                } else {
                    if (item[that.key] === value) {
                        flag = true;
                        list[level] = { name: item.name, url: item.url };
                    }
                }
            })
        }
        var flag = false;
        var list = [];
        for (let i = 0; i < menus.length; i++) {
            var menu = menus[i];
            if(flag){
                break;
            }
            list = [{ name: menu.name, url: '' }];
            deepTraversal(menu, 0);
        }
        return list;
    }

    return BreadNav;
});